package controllers

import (
	"WORKSPACE/models"
	"WORKSPACE/pkg/app"
	"WORKSPACE/pkg/e"
	"WORKSPACE/services"
	"encoding/json"
	"fmt"
	"github.com/gin-gonic/gin"
)

func GetAllemployess(c *gin.Context)  {
	err, info, total := services.GetAllemployess()
	if err !=nil{
		app.Error(c,e.ERROR,err,err.Error())
		return
	}
	app.OK(c, map[string]interface{}{"value": info, "total": total},"查询成功")
}

func Delemployess(c *gin.Context){
	id := "-1"
	if arg := c.Query("Eno"); arg != "" {
		id = arg
		fmt.Println(id)
	}
	if id == "-1"{
		app.INFO(c,30001,"参数错误")
		return
	}

	err := services.Delemployess(id)
	if err !=nil{
		app.Error(c,e.ERROR,err,err.Error())
		return
	}
	app.OK(c, map[string]interface{}{},"删除成功")
}

func Updateemployess(c *gin.Context){
	data, _ := c.GetRawData()
	var m map[string]string
	_ = json.Unmarshal(data, &m)
	eno := m["Eno"]
	ename := m["Ename"]
	esex := m["Esex"]
	ebirthday := m["Ebirthday"]
	edepartment := m["Edepartment"]
	etype := m["Etype"]
	eleavel := m["Eleavel"]
	etitle := m["Etitle"]
	eeduction := m["Eeduction"]
	enation := m["Enation"]
	enative := m["Enative"]
	eaddress := m["Eaddress"]
	ejion := m["Ejion"]

	if m["Eno"] == ""{
		app.INFO(c, 30000, "参数非法")
		return
	}

	err := services.Updateemployess(models.Employess{ Eno: eno, Ename: ename, Esex: esex, Ebirthday: ebirthday,  Edepartment: edepartment, Etype: etype, Eleavel: eleavel,Etitle:etitle,Eeduction:eeduction,Enation:enation,Enative:enative,Eaddress:eaddress,Ejion:ejion})

	if err !=nil{
		app.Error(c,e.ERROR,err,err.Error())
		return
	}
	app.OK(c, map[string]interface{}{},"更新成功！")
}

func Addemployess(c *gin.Context){
	data, _ := c.GetRawData()
	var m map[string]string
	_ = json.Unmarshal(data, &m)
	eno := m["Eno"]
	ename := m["Ename"]
	esex := m["Esex"]
	ebirthday := m["Ebirthday"]
	edepartment := m["Edepartment"]
	etype := m["Etype"]
	eleavel := m["Eleavel"]
	etitle := m["Etitle"]
	eeduction := m["Eeduction"]
	enation := m["Enation"]
	enative := m["Enative"]
	eaddress := m["Eaddress"]
	ejion := m["Ejion"]

	if m["Eno"] == ""{
		app.INFO(c, 30000, "参数非法")
		return
	}
	err := services.Addemployess(models.Employess{ Eno: eno, Ename: ename, Esex: esex, Ebirthday: ebirthday,  Edepartment: edepartment, Etype: etype, Eleavel: eleavel,Etitle:etitle,Eeduction:eeduction,Enation:enation,Enative:enative,Eaddress:eaddress,Ejion:ejion})
	fmt.Println(err)
	if err !=nil{
		app.Error(c,e.ERROR,err,err.Error())
		return
	}
	app.OK(c, map[string]interface{}{},"添加成功！")
}